/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2024-03-15 10:39:50
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-03-18 16:20:59
 * @FilePath: \dm\云课堂\js\details.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
$(function () {
    // 判断是否登录
    if (localStorage.getItem('userInfo')) {
        $('#userIn > .tips, .glyphicon').remove();
        var userInfo = JSON.parse(localStorage.getItem('userInfo'));
        $('#userIn').append(`
                    <div class="userIn">
                        <div class="userImg">
                            <img src="./images/userAvatar_1431_微信图片_20231103095832-1754844961339080704.jpg" alt="">
                        </div>
                        <div class="userName">
                            <span>${userInfo.nickname}</span>
                            <div class="userVip">
                            <img data-v-5c89a100="" src="" alt="">
                            青铜会员
                        </div>
                        </div>
                    </div>
                    `);
                    $('.goin').append(`
                    <div class="userIn">
                        <div class="userImg">
                            <img src="./images/userAvatar_1431_微信图片_20231103095832-1754844961339080704.jpg" alt="">
                        </div>
                        <div class="userName">
                            ${userInfo.nickname}
                        </div>
                        <div class="userVip">
                            <img data-v-5c89a100="" src="" alt="">
                            青铜会员
                        </div>
                    </div>
                    `);
    }
    // 请求下拉列表数据
    $.ajax({
        url: `https://wkt.myhope365.com//weChat/applet/subject/list`,
        type: 'post',
        headers: {
            'Content-type': 'application/json',
        }, //请求头
        data: JSON.stringify({
            "enable": 1
        }),
        success(res) {
            // console.log(res);
            if (res.code == 0) {
                res.rows.forEach(item => 
                    $('.pull-down').append(`<li><a href="#">${item.title}</a></li>`)
                )
            }
        },
        error: function (err) {
            console.log(err) //返回的数据
        }, //失败后
    })
    // 下拉列表
    // 当鼠标移入 .pullDown 元素时，显示下拉列表  
    $('.none').hover(
        function() {
            $('.pull-down').stop().slideToggle();
        },
        function() {
            $('.pull-down').stop().slideToggle();
        }
    )
    $('.content')
    // 登录与注册
    $('.user').click(function() {
        $(this).addClass('color_text').siblings('.pow').removeClass('color_text');
        $('#form-group').remove();
        $('.module_body').append(`
            <form class="form-group" id="form-group" action="">
                <div class="form-group input_user">
                    <img src="./images/用户名.png" alt="">
                    <input class="form-control" type="text" placeholder="请输入账户名">
                </div>
                <div class="form-group input_pow">
                    <img src="./images/密码.png" alt="">
                    <input class="form-control" type="password" placeholder="请输入密码">
                </div>
                <div class="text-right">
                    <button class="form-control btnIn" type="submit" style="color: #fff; background-color: #00CF8C; margin-top: 25px; margin-bottom: 20px;">登录</button>
                </div>
                <a href="" class="register href_a">立刻注册！</a>
                <a href="" class="forgot href_a">忘记密码？</a>
            </form>
        `);
    })
    $('.pow').click(function() {
        $(this).addClass('color_text').siblings('.user').removeClass('color_text');
        $('#form-group').remove();
        $('.module_body').append(`
            <form class="form-group" id="form-group" action="">
                <div class="form-group input_user">
                    <img src="./images/用户名.png" alt="">
                    <input class="form-control" type="text" placeholder="请输入手机号">
                </div>
                <div class="form-group input_pow verification">
                    <img src="./images/密码.png" alt="">
                    <input class="form-control" type="password" placeholder="请输入验证码" style="width: 158px;">
                    <button>获取验证码</button>
                </div>
                <div class="text-right">
                    <button class="form-control btnIn_1" type="submit" style="color: #fff; background-color: #00CF8C; margin-top: 0px; margin-bottom: 20px;">登录</button>
                </div>
                <a href="" class="register href_a">立刻注册！</a>
                <a href="" class="forgot href_a">忘记密码？</a>       
            </form> 
        `)
    })
    $('.btnIn').click(function () {
        var user = $('#user').val();
        var pow =  $('#pow').val();
        $.ajax({
            url: `https://wkt.myhope365.com/pcUser/login`,
            type: 'POST',
            data: {
                username: user,
                password: pow,
                rememberMe: true,
            },
            success(res) {
                // console.log(res);
                if (res.code == 500) {
                    alert('用户不存在/密码错误');
                }
                if (res.code == 0) {
                    alert('成功登录');
                    $('#register').modal('hide');
                    $('#userIn > .tips, .glyphicon').remove();
                    $('.goin').append(`
                    <div class="userIn">
                        <div class="userImg">
                            <img src="./images/userAvatar_1431_微信图片_20231103095832-1754844961339080704.jpg" alt="">
                        </div>
                        <div class="userName">
                            ${res.userInfo.nickname}
                        </div>
                        <div class="userVip">
                            <img data-v-5c89a100="" src="" alt="">
                            青铜会员
                        </div>
                    </div>
                    `);
                }
                // 本地存储
                localStorage.setItem('userInfo', JSON.stringify(res.userInfo));
            },
            error: function (err) {
                console.log(err) //返回的数据
            }, //失败后
        })
    })
    // 登录退出下拉菜单
    $('.goin').hover(
        function() {
            $('.topDown').stop().slideToggle();
        },
        function() {
            $('.topDown').stop().slideToggle();
        }
    );
    $('.out').click(function () {
        localStorage.removeItem('userInfo');
        window.location.reload();
    })
    // 搜索请求
    var tap = window.location.href.split('?')[1];
    if (tap) {
        $('.search').click(function () {
            var event = event || window.event;
            event.preventDefault();
            // window.location.reload();
        })
        $.ajax({
            url: `https://wkt.myhope365.com//pc/course/search/keyword?pageNum=1&pageSize=15`,
            type: 'GET',
            success(res) {
                console.log(res);
                if (res.code == 0) {
                   var page =  Math.floor(res.total / 15) + 1;
                   for (let i = 1; i <= page; i++) {
                        if (i == 1) {
                            $('.ant-pagination-next').before(`
                            <div class="divBtn focalPoint"><a>${i}</a></div>
                        `)
                        }
                        $('.ant-pagination-next').before(`
                            <div class="divBtn"><a>${i}</a></div>
                        `)
                   }
                    res.courseList.forEach((item, index) => {
                        // if (index == 16) {
                        //     page = 1;
                        //     window.location.reload();
                        //     ul_List.empty();
                        // }
                        $('.ul_List').append(`
                            <li>
                                <div class="boxMin">
                                    <div class="listImg">
                                    <img src="${item.coverFileUrl}" alt="">
                                    </div>
                                    <div class="listText">
                                        <p class="title_p">
                                            ${item.courseTitle}
                                        </p>
                                        <p class="course">
                                            共 ${item.subSectionNum} 节课 | ${item.participationsCount} 人报名
                                        </p>
                                        <div class="price">
                                            <span class="p"><b>${item.isFree == 1 ? '免费' : ''}</b><b style="color: red;">${item.isFree == 0 && item.isDiscount == 1 ? '￥' + item.discountPrice : ''}</b></span>
                                            <span><del>${item.isFree == 0 && item.isDiscount == 1 ? '￥' + item.coursePrice : ''}</del><span>
                                        </div>
                                    </div>
                                </div>
                            </li>
                        `)
                    })
                }
            },
            error: function (err) {
                console.log(err) //返回的数据
            }, //失败后
        })
        
    }
})
